package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.PackDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackService;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ShippingServiceImpl implements ShippingService {


    @Autowired
    private ShippingDao shippingDao;

    @Autowired
    private PackService packService;

    @Autowired
    private ExportService exportService;

    @Autowired
    private PackDao packDao;

    //委托单的分页
    @Override
    public PageInfo<Shipping> findByPage(ShippingExample shippingExample, int pageNum, int pageSize) {
        //1.设置当前页与页面大小
        PageHelper.startPage(pageNum,pageSize);
        //2 .查询数据
        List<Shipping> shippingList = shippingDao.selectByExample(shippingExample);

        //3. 构建PageInfo
        PageInfo<Shipping> pageInfo = new PageInfo<>(shippingList);
        return pageInfo;
    }


    //根据条件查询
    @Override
    public List<Shipping> findAll(ShippingExample shippingExample) {
        List<Shipping> shippingList = shippingDao.selectByExample(shippingExample);
        return shippingList;
    }


    //根据主键查询委托单
    @Override
    public Shipping findById(String id) {
        return shippingDao.selectByPrimaryKey(id);
    }


    @Override
    public void save(Shipping shipping) {
        shipping.setCreateTime(new Date());
        String shippingOrderId = shipping.getShippingOrderId();
        Pack pack = packService.findById(shippingOrderId);
        pack.setState(1l);
        packDao.updateByPrimaryKeySelective(pack);
        shipping.setState(0);
        shippingDao.insertSelective(shipping);
    }

    @Override
    public void update(Shipping shipping) {

    }

    @Override
    public void delete(String id) {
        Shipping shipping = findById(id);
        String shippingOrderId = shipping.getShippingOrderId();
        Pack pack = packService.findById(shippingOrderId);
        pack.setState(0l);
        packDao.updateByPrimaryKeySelective(pack);
        shippingDao.deleteByPrimaryKey(shippingOrderId);
    }
   

}
